<!--
 * @Author: shiweizhen 787632169@qq.com
 * @Date: 2025-07-04 16:58:41
 * @LastEditors: shiweizhen 787632169@qq.com
 * @LastEditTime: 2025-07-07 11:18:56
 * @Description: 
 * Copyright (c) 2025-present HBIS Digital Technology Co.,Ltd. All rights reserved. 
-->
<template>
  <div class="h-full w-full">
    <div class="mb-4">请选择筛选内容</div>
    <el-select v-model="activeFilter" placeholder="请选择筛选内容" @change="handleSelectChange">
      <el-option v-for="(item, index) in filterList" :key="index" :label="item.label" :value="item.id" />
    </el-select>
  </div>
</template>

<script setup lang="ts">
import { filterSheet } from '@/api/univer-sheet';
import { FilterType } from '@/api/univer-sheet/types';
import { FUniver } from '@univerjs/presets';

const props = defineProps({
  univerAPIInstance: {
    type: Object as () => FUniver,
  }
})

const univerInstance = props.univerAPIInstance
const fWorkbook = univerInstance?.getActiveWorkbook()
const fWorksheet = fWorkbook?.getActiveSheet();
const fWorkCell = fWorkbook?.getActiveCell()
const fSelection = fWorksheet?.getSelection();
const activeRange = fSelection?.getActiveRange();
const filterList = ref<{ label: string, value: number, id: number }[]>([])
const activeFilter = ref<number>()

const getFilterList = async () => {
  const params: FilterType = {
    workbookDataId: fWorkbook?.getId()!,
    workbookSheetId: fWorksheet?.getSheetId()!,
    excelRange: activeRange?.getA1Notation()!,
  }
  const res = await filterSheet(params)
  filterList.value = res.data
}

const handleSelectChange = (val: number) => {
  const find = filterList.value.find(item => item.id === val)
  if (find) {
    fWorkCell?.setValue({ v: find.value, custom: { ...fWorkCell?.getCustomMetaData(), filterId: val }, s: { bg: { rgb: '#fde8e8' } } }).setNumberFormat('#,##0')
  }
}

onMounted(async () => {
  await getFilterList()
  activeFilter.value = fWorkCell?.getCustomMetaData()?.filterId
})
</script>